3 namespace MediaWiki\Widget
;
6 * Check matrix widget. Displays a matrix of checkboxes for given options
8 * @copyright 2018 MediaWiki Widgets Team and others; see AUTHORS.txt
11 class CheckMatrixWidget
extends \OOUI\Widget
{
14 protected $columns = [];
16 protected $tooltips = [];
17 protected $values = [];
18 protected $forcedOn = [];
19 protected $forcedOff = [];
22 * CheckMatrixWidget constructor
24 * Operates similarly to MultiSelectWidget, but instead of using an array of
25 * options, uses an array of rows and an array of columns to dynamically
26 * construct a matrix of options. The tags used to identify a particular cell
27 * are of the form "columnName-rowName"
29 * @param array $config Configuration array with the following options:
31 * - Required list of columns in the matrix.
33 * - Required list of rows in the matrix.
35 * - Accepts array of column-row tags to be displayed as enabled but unavailable to change
37 * - Accepts array of column-row tags to be displayed as disabled but unavailable to change.
39 * - Optional array mapping row label to tooltip content
41 * - Optional CSS class used on tooltip container span. Defaults to mw-icon-question.
43 public function __construct( array $config = [] ) {
44 // Configuration initialization
46 parent
::__construct( $config );
48 $this->name
= $config['name'] ??
null;
49 $this->id
= $config['id'] ??
null;
52 $this->rows
= $config['rows'] ??
[];
53 $this->columns
= $config['columns'] ??
[];
54 $this->tooltips
= $config['tooltips'] ??
[];
56 $this->values
= $config['values'] ??
[];
58 $this->forcedOn
= $config['forcedOn'] ??
[];
59 $this->forcedOff
= $config['forcedOff'] ??
[];
62 $table = new \OOUI\
Tag( 'table' );
63 $tr = new \OOUI\
Tag( 'tr' );
65 $tr->appendContent( $this->getCellTag( "\u{00A0}" ) );
66 foreach ( $this->columns
as $columnLabel => $columnTag ) {
68 $this->getCellTag( $columnLabel )
71 $table->appendContent( $tr );
73 // Build the options matrix
74 foreach ( $this->rows
as $rowLabel => $rowTag ) {
75 $table->appendContent(
76 $this->getTableRow( $rowLabel, $rowTag )
81 $this->addClasses( [ 'mw-widget-checkMatrixWidget' ] );
82 $this->appendContent( $table );
86 * Get a formatted table row for the option, with
89 * @param string $label Row label
90 * @param string $tag Row tag name
91 * @return \OOUI\Tag The resulting table row
93 private function getTableRow( $label, $tag ) {
94 $row = new \OOUI\
Tag( 'tr' );
95 $tooltip = $this->getTooltip( $label );
96 $labelFieldConfig = $tooltip ?
[ 'help' => $tooltip ] : [];
98 $labelField = new \OOUI\
FieldLayout(
99 new \OOUI\
Widget(), // Empty widget, since we don't have the checkboxes here
103 ] +
$labelFieldConfig
105 $row->appendContent( $this->getCellTag( $labelField ) );
107 // Build checkbox column cells
108 foreach ( $this->columns
as $columnTag ) {
109 $thisTag = "$columnTag-$tag";
111 // Construct a checkbox
112 $checkbox = new \OOUI\
CheckboxInputWidget( [
114 'name' => $this->name ?
"{$this->name}[]" : null,
115 'id' => $this->id ?
"{$this->id}-$thisTag" : null,
116 'selected' => $this->isTagChecked( $thisTag ),
117 'disabled' => $this->isTagDisabled( $thisTag ),
120 $row->appendContent( $this->getCellTag( $checkbox ) );
126 * Get an individual cell tag with requested content
128 * @param string $content Content for the <td> cell
129 * @return \OOUI\Tag Resulting cell
131 private function getCellTag( $content ) {
132 $cell = new \OOUI\
Tag( 'td' );
133 $cell->appendContent( $content );
138 * Check whether the given tag's checkbox should
141 * @param string $tagName Tag name
142 * @return boolean Tag should be checked
144 private function isTagChecked( $tagName ) {
145 // If the tag is in the value list
146 return in_array( $tagName, (array)$this->values
, true ) ||
147 // Or if the tag is forced on
148 in_array( $tagName, (array)$this->forcedOn
, true );
152 * Check whether the given tag's checkbox should
155 * @param string $tagName Tag name
156 * @return boolean Tag should be disabled
158 private function isTagDisabled( $tagName ) {
160 // If the entire widget is disabled
161 $this->isDisabled() ||
162 // If the tag is 'forced on' or 'forced off'
163 in_array( $tagName, (array)$this->forcedOn
, true ) ||
164 in_array( $tagName, (array)$this->forcedOff
, true )
169 * Get the tooltip help associated with this row
171 * @param string $label Label name
172 * @return string Tooltip. Null if none is available.
174 private function getTooltip( $label ) {
175 return $this->tooltips
[ $label ] ??
null;
178 protected function getJavaScriptClassName() {
179 return 'mw.widgets.CheckMatrixWidget';
182 public function getConfig( &$config ) {
184 'name' => $this->name
,
186 'rows' => $this->rows
,
187 'columns' => $this->columns
,
188 'tooltips' => $this->tooltips
,
189 'forcedOff' => $this->forcedOff
,
190 'forcedOn' => $this->forcedOn
,
191 'values' => $this->values
,
193 return parent
::getConfig( $config );